<?php

namespace App\backend\controllers\coupon;


use App\backend\controllers\AdminBaseController;
use App\backend\services\CouponServices;
use App\backend\services\SubsidyServices;
use App\common\models\Coupon;
use App\common\models\CouponUseLog;
use App\common\models\Subsidy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SubsidyController extends AdminBaseController
{
    protected $class_name = 'Subsidy';


    public function lists(Request $request)
    {

        $data = $request->all();
        $validator = Validator::make(
            $data,
            Subsidy::rules(),
            Subsidy::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = SubsidyServices::getLists($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 专用券列表
     */
    public function dedicatedCouponLists(Request $request)
    {

//        $data = self::getStoreData($this->class_name, 'getGoodsList', 2);
//        if ($data) {
//            return $data;
//        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getDedicatedCouponList($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 获取 - 通用券详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function universalCouponDetail(Request $request){
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getUniversalCouponDetail($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 获取 - 专用券详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function dedicatedCouponDetail(Request $request){
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getDedicatedCouponDetail($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 添加/编辑通用卡券
     */
    public function modifyUniversalCoupon(Request $request)
    {

        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::modify_universal_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::modifyUniversalCoupon($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 添加/编辑专用卡券
     */
    public function modifyDedicatedCoupon(Request $request)
    {

        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::modify_dedicated_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::modifyDedicatedCoupon($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 删除卡券
     */
    public function delCoupon(Request $request)
    {
//        $data = self::getStoreData('Goods','removalDelGoods', 2);
//        if ($data) {
//            return $data;
//        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::updateGoodStatus($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 发券接口
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendCoupon(Request $request){
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CouponUseLog::add_rules(),
            CouponUseLog::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::sendCoupon($data);
        if (isset($list['msg'])) {
            return errorJson($list['msg'], []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 使用记录
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function useLog(Request $request){

//        $data = self::getStoreData($this->class_name, 'getGoodsList', 2);
//        if ($data) {
//            return $data;
//        }
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CouponUseLog::rules(),
            CouponUseLog::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getUseLog($data);
        return successJson('获取成功!', $list);
    }


    /**
     * 商户券列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function storeCouponLists(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getStoreCouponLists($data);
        return successJson('获取成功!', $list);
    }

    public function storeDetail(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getStoreCouponDetail($data);
        return successJson('操作成功！', $list);
    }

    /**
     * 设置停售
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function haltSales(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::haltSales($data);
        return successJson('操作成功！', $list);
    }
}
